"""
行程规划管理后台
"""
from django.contrib import admin
from .models import Itinerary, DayPlan, Activity


class ActivityInline(admin.TabularInline):
    """活动内联管理"""
    model = Activity
    extra = 1
    fields = ('attraction', 'start_time', 'end_time', 'transport_mode', 'order', 'notes')


class DayPlanInline(admin.TabularInline):
    """每日计划内联管理"""
    model = DayPlan
    extra = 1
    fields = ('day_number', 'date')


@admin.register(Itinerary)
class ItineraryAdmin(admin.ModelAdmin):
    list_display = ('title', 'user', 'start_date', 'end_date', 'total_budget', 'created_at')
    list_filter = ('created_at', 'start_date', 'end_date')
    search_fields = ('title', 'user__username')
    ordering = ('-created_at',)
    inlines = [DayPlanInline]
    fieldsets = (
        ('基本信息', {'fields': ('user', 'title')}),
        ('时间安排', {'fields': ('start_date', 'end_date')}),
        ('预算', {'fields': ('total_budget',)}),
    )


@admin.register(DayPlan)
class DayPlanAdmin(admin.ModelAdmin):
    list_display = ('itinerary', 'day_number', 'date')
    list_filter = ('date',)
    search_fields = ('itinerary__title',)
    ordering = ('itinerary', 'day_number')
    inlines = [ActivityInline]


@admin.register(Activity)
class ActivityAdmin(admin.ModelAdmin):
    list_display = ('day_plan', 'attraction', 'start_time', 'end_time', 'transport_mode', 'order')
    list_filter = ('transport_mode', 'start_time')
    search_fields = ('attraction__name', 'day_plan__itinerary__title')
    ordering = ('day_plan', 'order', 'start_time')


